<template>
  <div class="dashnoard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登陆账户设置">
            <!-- 放置内容 -->
            <el-form ref="ruleForm" :model="userinfo" :rules="rules" label-width="120px" style="width:50%; padding-top:40px">
              <el-row type="flex" justify="end">

                <el-form-item label="用户名:" prop="username">
                  <el-input v-model="userinfo.username" style="width:300px; margin-left:20px;" />
                </el-form-item>

              </el-row>
              <el-row type="flex" justify="end">
                <el-form-item label="密码:" prop="password2">
                  <el-input v-model="userinfo.password2" type="password" style="width:300px; margin-left:20px;" />
                </el-form-item>
              </el-row>
              <el-row type="flex" justify="center">

                <el-form-item>
                  <el-button v-loading="loading" type="primary" style="margin-left:40px" @click="saveUser">更新</el-button>
                  <el-button size="small">取消</el-button>
                </el-form-item>

              </el-row>

            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 打印功能 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${this.$route.params.id}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 放置内容 -->
            <!-- <UserInfo /> -->
            <!-- vuejs中内置了一个组件，component可以是任何组件 -->
            <!-- <el-button type="primary" @click="UserInfo='el-table'">切换组件</el-button>
            <el-button @click="UserInfo = 'UserInfo'">切换组件</el-button> -->
            <!-- 个人信息 -->
            <component :is="UserInfo" />
            <!-- 动态组件可以切换组件 -->
          </el-tab-pane>
          <el-tab-pane label="岗位信息" :lazy="true">
            <!-- 放置内容 -->
            <!-- 打印功能 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${this.$route.params.id}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 岗位信息 -->
            <component :is="JobInfo" ref="getJobDetails" />

          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDatailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import UserInfo from '../compnents/user-info.vue'
import JobInfo from '../compnents/job-info.vue'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      UserInfo: 'UserInfo',
      JobInfo: 'JobInfo',
      userinfo: {
        username: '',
        password2: '' // 为什么叫password2,因为读取出来的password是密文
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigge: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度为6-9位', trigger: 'blur' }
        ]
      },
      loading: false

    }
  },
  created() {
    this.getUserDatailById(this.$route.params.id)
  },
  methods: {
    async getUserDatailById(id) {
      this.userinfo = await getUserDatailById(id)
    },
    async saveUser() {
      try {
        this.loading = true
        await this.$refs.ruleForm.validate
        await saveUserDetailById({ ...this.userinfo, password: this.userinfo.password2, id: this.$route.params.id })
        this.loading = false
        this.$message.success('保存成功')
      } catch (error) {
        this.loading = false
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
